package com.ftg.learn.chapter09.base02.util;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 案例
 *
 * @author 布衣
 */
public class JdbcUtil<E> {

    private final String userName = "root";
    private final String passWord = "123456";
    private final String url = "jdbc:mysql://localhost:3306/exam?serverTimezone=Asia/Harbin";

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, userName, passWord);
    }

    private void myClose(ResultSet rs,PreparedStatement stmt, Connection con){
        try {
            if(null != rs){
                rs.close();
            }
            if(null != stmt){
                stmt.close();
            }
            if(null != con){
                con.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void setParameter(PreparedStatement stmt,Object...params) throws SQLException {
        //预编译sql语句，里的?赋值
        for (int i = 0; i < params.length; i++) {
            stmt.setObject(i + 1, params[i]);
        }
    }

    private List<Map<String, Object>> handlerResultSet(ResultSet resultSet) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<>();
        while (resultSet.next()) {
            Map<String, Object> map = new HashMap<>();
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                map.put(metaData.getColumnLabel(i + 1), resultSet.getObject(i + 1));
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 查询
     *
     * @param sql    语句
     * @param params 参数
     * @return List 结果 集
     */
    public List<Map<String, Object>> query(String sql, Object... params) {
        List<Map<String, Object>> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            statement = connection.prepareStatement(sql);
            this.setParameter(statement,params);
            resultSet = statement.executeQuery();
            return this.handlerResultSet(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           this.myClose(resultSet, statement, connection);
        }
        return list;
    }
    /**
     * 更新
     *
     * @param sql    语句
     * @param params 参数
     * @return 影响行数
     */
    public int update(String sql, Object... params) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //会话得到了
            connection = DriverManager.getConnection(url, userName, passWord);
            statement = connection.prepareStatement(sql);
            this.setParameter(statement,params);
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.myClose(null, statement, connection);
        }
        return -1;
    }

    public static void main(String[] args) {
        JdbcUtil<Object> jdbcUtil = new JdbcUtil<>();
        jdbcUtil.update("insert into students(name,age) values(?,?)", new Object[]{"张三", 18, 1});
    }

}
